let mysqlDump = require('mysqldump');
let mysqlConfig = require('../config');
let schedule = require('node-schedule');
let settingDao = require('../model/admin/setting')


//数据库定时备份
function backupMysql(){
    try {
        let mysqlDatabase = mysqlConfig.database;

        mysqlDump({
                host: mysqlDatabase.host,
                user: mysqlDatabase.user,
                password: mysqlDatabase.password,
                database: mysqlDatabase.database,
                dest: './public/backup.sql'
            }),
            function (err) {
                console.log(err)
            }
    } catch (error) {
        console.log(error)
    }

}

let rule1 = '30 30 1 * * *'
module.exports =  async function () {
    let getBackupStatus  =  await settingDao.getBackupStatus();
    schedule.scheduleJob(rule1, function(){ 
        if(getBackupStatus.backupStatus==1){
            backupMysql();  
        }
       
    }); 
        
}


